<?php

namespace app\xwam\model\user;

/**
 * Description of UserModel
 *
 * @author Su 2015-3-5
 */
class AdminModel {

    const TABLE = 'user_admin';

    /**
     * 保存用户
     * @param int $admin_id 用户编号
     * @return array
     * @version 1.0 2015-3-6 by sutroon
     */
    public function saveRecord($data) {

        $isAdd = empty($data['id']);
        $validate = new \app\xwam\validate\AdminValidate();
        if (!$validate->scene($isAdd ? true : 'edit')->check($data)) {
            //LogModel::saveUserOperate(false, sprintf('%s用户账户失败 user=%s, %s', ($is_edit ? '编辑' : '新增'), $data['user_name'], implode('; ', $validate->getError())), $admin_id, ($is_edit ? LogModel::ACTION_EDIT : LogModel::ACTION_ADD));
            return xsfn_message(false, $validate->getError());
        }
        if ($isAdd) {
            unset($data['id']);
            $effectRows = db(self::TABLE)->insert($data);
        } else {
            unset($data['user_name']);
            $effectRows = db(self::TABLE)->update($data);
        }
        //LogModel::saveUserOperate($result, sprintf('%s用户账户%s user=%s', ($is_edit ? '编辑' : '新增'), '{%}', $data['user_name']), $admin_id, ($is_edit ? LogModel::ACTION_EDIT : LogModel::ACTION_ADD));
        return xsfn_message(true);
    }

    /**
     * 删除用户
     * @param string $id 主键编号,多个之间以逗号隔开
     * @param int $admin_id 操作员编号
     * @return array
     * @version 1.0 2015-6-10 SoChishun Added.
     */
    public function deleteRecord($id) {
        if (!$id) {
            return xsfn_message(false, '编号无效');
        }
        $result = db(self::TABLE)->where(['id', 'in', $id])->delete();
        //LogModel::saveUserOperate($status, "删除用户账户($id){%} $msg,操作员:$admin_id", $admin_id, LogModel::ACTION_DELETE);
        return xsfn_message($result);
    }

    public function setValue($id, $value, $field) {
        
    }

    public function switchValue($id, $field) {
        
    }

}
